package com.hnluchuan.bmjlearning.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import com.hnluchuan.bmjlearning.common.CustomerStatus;
import com.hnluchuan.bmjlearning.common.CustomerType;
import com.hnluchuan.bmjlearning.model.User;
import com.hnluchuan.bmjlearning.util.CustomDateUtil;
import com.hnluchuan.utils.common.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;
import org.apache.commons.lang3.StringUtils;
	
import com.hnluchuan.core.dao.BaseDAO;
import com.hnluchuan.bmjlearning.model.Customer;
import com.hnluchuan.bmjlearning.dto.CustomerDTO;
import com.hnluchuan.core.support.Page;

@Repository
public class CustomerDAO extends BaseDAO<Customer> {

    public List<Customer> find(CustomerDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From Customer n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getUser() != null) {
        	if (dto.getUser().getId() != null) {
	            hql.append(" and n.user.id = ? ");
	            paras.add(dto.getUser().getId());
        	}
        }			
        if (dto.getStatus() != null) {
            hql.append(" and n.status = ? ");
            paras.add(dto.getStatus());
        }
        if (StringUtils.isNotBlank(dto.getOrgName())) {
            hql.append(" and n.orgName like ? ");
            paras.add("%" + dto.getOrgName() + "%");
        }					
        if (StringUtils.isNotBlank(dto.getName())) {
            hql.append(" and n.name like ? ");
            paras.add("%" + dto.getName() + "%");
        }
        if (StringUtils.isNotBlank(dto.getMobile())) {
            hql.append(" and n.mobile = ? ");
            paras.add(dto.getMobile());
        }
        if (StringUtils.isNotBlank(dto.getNameOrMobile())){
            hql.append(" and (n.name like ? or n.mobile like ?) ");
            paras.add("%"+dto.getNameOrMobile()+"%");
            paras.add("%"+dto.getNameOrMobile()+"%");
        }
        if (dto.getProvince() != null) {
        	if (dto.getProvince().getId() != null) {
	            hql.append(" and n.province.id = ? ");
	            paras.add(dto.getProvince().getId());
        	}
        }			
        if (dto.getCity() != null) {
        	if (dto.getCity().getId() != null) {
	            hql.append(" and n.city.id = ? ");
	            paras.add(dto.getCity().getId());
        	}
        }			
        if (dto.getDistrict() != null) {
        	if (dto.getDistrict().getId() != null) {
	            hql.append(" and n.district.id = ? ");
	            paras.add(dto.getDistrict().getId());
        	}
        }			
        if (dto.getAgent() != null) {
        	if (dto.getAgent().getId() != null) {
	            hql.append(" and n.agent.id = ? ");
	            paras.add(dto.getAgent().getId());
        	}
        }
        if (StringUtils.isNotBlank(dto.getStartDate())){
            hql.append(" and date_format(n.created, '%Y %m %d') >= date_format(?, '%Y %m %d') ");
            paras.add(CustomDateUtil.getShortTimeDate(dto.getStartDate()));
        }
        if (StringUtils.isNotBlank(dto.getEndDate())){
            hql.append(" and date_format(n.created, '%Y %m %d') <= date_format(?, '%Y %m %d') ");
            paras.add(CustomDateUtil.getShortTimeDate(dto.getEndDate()));
        }
        if (dto.getType() != null){
            hql.append(" and n.type = ? ");
            paras.add(dto.getType());
        }
        hql.append(" order by n.id desc ");
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public Customer findOne(CustomerDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<Customer> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public int getTodayNewlyEnrolledCustomersCount(Long agentId){
        String hql = "Select count(*) from Customer c where c.agent.id = ? " +
                "and c.created >= ? and c.created <= ?";
        List<Object> paras = new ArrayList<>();
        paras.add(agentId);

        Date now = new Date();
        Date beginningOfADay = DateUtils.getBeginningOfADay(now);
        Date endOfADay = DateUtils.getEndOfADay(now);
        paras.add(beginningOfADay);
        paras.add(endOfADay);

        return countByHql(hql, paras.toArray());
    }

    public Customer findByUserId(Long userId){
        String hql = "From Customer c where c.user.id = ? and c.status = ?";
        List<Object> paras = new ArrayList<>();
        paras.add(userId);
        paras.add(CustomerStatus.Enabled.getValue());

        return super.findOne(hql, paras.toArray());
    }

    public List<Customer> findByAgentId(Long agentId, Page page){
        String hql = "From Customer c where c.agent.id = ?";
        List<Object> paras = new ArrayList<>();
        paras.add(agentId);

        return super.find(hql, paras.toArray(), page);
    }

    public int enableOrDisable(Long id, Integer status){
        String hql = "Update Customer c set status = ? where c.id = ?";
        List<Object> paras = new ArrayList<>();
        paras.add(status);
        paras.add(id);

        return super.executeUpdate(hql, paras.toArray());
    }

    @Override
	public Long create(Customer customer) {
	    if (customer.getCreated() == null) {    
	        customer.setCreated(new Date());
	    }
	    if (customer.getStatus() == null) {
            customer.setStatus(CustomerStatus.Enabled.getValue());
        }
        if (customer.getType() == null) {
            customer.setType(CustomerType.INACTIVE.getValue());
        }
		return super.create(customer);
	}

	@Override
	public void update(Customer customer) {
		super.update(customer);
	}
    
}
